﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Guline.Hoster.Controllers
{
    public class OrganizeController : BaseController
    {
        //
        // GET: /Organize/
        public ActionResult jsonList()
        {
            return Json(this.db.Organizes, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            return View(this.db.Organizes);
        }
        public ActionResult Create()
        {
            ViewBag.AuthorID= new SelectList(this.db.Accounts, "ID", "UserName");
            ViewBag.HostingPackageID = new SelectList(this.db.HostPackages, "ID", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Create(Guline.HosterModels.Object.Organize model)
        {
            ViewBag.AuthorID = new SelectList(this.db.Accounts, "ID", "UserName");
            ViewBag.HostingPackageID = new SelectList(this.db.HostPackages, "ID", "Name");
            if (ModelState.IsValid)
            {
                this.db.Organizes.Add(model);
                this.db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult Edit(int ID)
        {
            var organize = db.Organizes.Where(m => m.ID == ID).FirstOrDefault();
            ViewBag.AuthorID = new SelectList(this.db.Accounts, "ID", "UserName",selectedValue:organize.AuthorID);
            ViewBag.HostingPackageID = new SelectList(this.db.HostPackages, "ID", "Name",selectedValue:organize.HostingPackageID);
            return View(organize);
        }
        [HttpPost]
        public ActionResult Edit(Guline.HosterModels.Object.Organize model)
        {
            ViewBag.AuthorID = new SelectList(this.db.Accounts, "ID", "UserName",selectedValue:model.AuthorID);
            ViewBag.HostingPackageID = new SelectList(this.db.HostPackages, "ID", "Name",selectedValue:model.HostingPackageID);
            if (ModelState.IsValid)
            {

                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult Delete(int ID)
        {
            var oganize = db.Organizes.Where(m => m.ID == ID).FirstOrDefault();
            db.Organizes.Remove(oganize);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult editStatus(int ID)
        {
            var organize = db.Organizes.Where(m => m.ID == ID).FirstOrDefault();
            if (organize.Status == 0)
            {
                organize.Status = 1;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                organize.Status = 0;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

    }
}
